package com.example.imtoken.modules.main.wallet.manage.admin

import android.content.Intent
import android.text.InputType
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.imtoken.R
import com.example.imtoken.core.OnBottomSheetDialogFragmentListener
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.databinding.ActivityAdminManageBinding
import com.example.imtoken.modules.login.LoginActivity
import com.example.imtoken.modules.main.wallet.add_token.AddTokenActivity
import com.example.imtoken.modules.main.wallet.manage.admin.details.AdminDetailsActivity
import com.example.imtoken.modules.main.wallet.manage.admin.pass_tips.PassWordTipsActivity
import com.example.imtoken.modules.private_import.backup.PrivateBackupActivity
import com.example.imtoken.view.exit.ExitFragment
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.xuexiang.xui.widget.dialog.materialdialog.GravityEnum
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog

/**
 * 管理身份钱包
 */
class AdminManageActivity :
    BaseActivity<AdminManageViewModel, ActivityAdminManageBinding>(AdminManageViewModel::class.java) {

    private val key by lazy {
        intent.getStringExtra("key")
    }

    override fun initLayoutId(): Int {
        return R.layout.activity_admin_manage
    }

    override fun init() {
        initTitle()
        initClick()
        initObserve()
        if (key == null) {
            viewModel.getDefAccount()
        } else {
            viewModel.getDefAccount(key)
        }
    }

    private fun initObserve() {
        viewModel.getDefAccountData().observe(this) {
            dataBinding.entity = it
        }
        viewModel.isSuccess.observe(this) {
            if (it) {
                val intent = Intent(this, LoginActivity::class.java)
                intent.putExtra("exit", true)
                ActivityUtils.startActivity(intent)
            }
        }
    }

    private fun initClick() {
        dataBinding.include16.ivHeadBack.setOnClickListener {
            setResult(200)
            this.finish()
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.llBackupWallet) {
            //备份钱包
            walletPass()
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.llAddToken) {
            //跳转添加代币
            val intent = Intent(this, AddTokenActivity::class.java)
            intent.putExtra("addTokenType", 1)
            ActivityUtils.startActivity(intent)
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.llPassTips) {
            val intent = Intent(this, PassWordTipsActivity::class.java)
            intent.putExtra("key", key)
            ActivityUtils.startActivity(intent)
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.llAdminManage) {
            val intent = Intent(this, AdminDetailsActivity::class.java)
            intent.putExtra("key", key)
            startActivityForResult(intent, 0)
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.tvExit) {
            //退出
            val dialog = ExitFragment()
            dialog.setOnBottomSheetDialogFragmentListener(object :
                OnBottomSheetDialogFragmentListener {
                override fun dialog(dialog: BottomSheetDialogFragment) {
                    dialog.dismiss()
                    MaterialDialog.Builder(this@AdminManageActivity)
                        .title(R.string.inputPassWordTips)
                        .titleGravity(GravityEnum.CENTER)
                        .content(R.string.inputPassWordTips1)
                        .contentGravity(GravityEnum.CENTER)
                        .inputType(InputType.TYPE_TEXT_VARIATION_PASSWORD)
                        .input(
                            getString(R.string.passWord),
                            ""
                        ) { _, input ->
                            if (input.toString().isNotEmpty()) {
                                if (viewModel.walletPass(input.toString())) {
                                    viewModel.exitAdmin()
                                }
                            } else {
                                ToastUtils.showShort(R.string.inputPassWordTips)
                            }
                        }
                        .positiveText(R.string.confirm)
                        .negativeText(R.string.cancel)
                        .cancelable(false)
                        .show().contentView?.textSize = 14f
                }

            })
            dialog.show(supportFragmentManager, "dialog")
        }
    }


    fun walletPass() {
        MaterialDialog.Builder(this)
            .title(R.string.walletPass)
            .inputType(InputType.TYPE_TEXT_VARIATION_PASSWORD)
            .input(
                getString(R.string.inputWalletPassTips),
                ""
            ) { _, input ->
                if (input.toString().isNotEmpty()) {
                    if (viewModel.walletPass(input.toString())) {
                        //备份钱包
                        val intent = Intent(this, PrivateBackupActivity::class.java)
                        intent.putExtra(
                            "mnemonic",
                            viewModel.getDefAccountData().value?.mnemonic
                        )
                        ActivityUtils.startActivity(intent)
                    }
                } else {
                    ToastUtils.showShort(R.string.pleaseEnterPassWord)
                }
            }
            .positiveText(R.string.confirm)
            .negativeText(R.string.cancel)
            .cancelable(false)
            .show()
    }

    private fun initTitle() {
        dataBinding.include16.tvHeadTitles.text = getString(R.string.manageAdminWallet)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 200) {
            if (key == null) {
                viewModel.getDefAccount()
            } else {
                viewModel.getDefAccount(key)
            }
        }
    }

}